-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Reduce allocation in TextWriter.NewLine #121508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-io |
It's reasonably common for a codebase to pick a specific newline string and set it regardless of OS, but currently that ends up allocating on every OS. Two changes: 1. When setting TextWriter.NewLine to the existing value, make it a nop to avoid unnecessarily calling ToCharArray. 2. When setting TextWriter.NewLine to "\n" on Windows or to "\r\n" on Unix, use a cached array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes TextWriter.NewLine setter to reduce allocations in common scenarios where codebases set a specific newline string regardless of OS. The changes include:
- Making the setter a no-op when setting to the same value
- Caching arrays for "\n" and "\r\n" (the non-default OS newline strings)
It's reasonably common for a codebase to pick a specific newline string and set it regardless of OS, but currently that ends up allocating on every OS each call to set_NewLine.
Two changes: